﻿using System;
using System.Collections.Generic;
using System.Linq;
using FangChanCrm.Services.Interface;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using NPOI.SS.Formula.Functions;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using FangChanCrmEF;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace FangChanCrm.Controllers
{
    public class QuanXianController : Controller
    {
        private readonly It_QuanXianBiaoService t_QuanXianBiaoService;
        private readonly It_QuanXianPeiZhiBiaoService t_QuanXianPeiZhiBiaoService;
        private readonly It_RenYuanQuanXianService t_RenYuanQuanXianService;
        private readonly It_BuMenBiaoService t_BuMenBiaoService;
        private readonly It_GangWeiBiaoService t_GangWeiBiaoService;
        private readonly It_ZhanQuBiaoService t_ZhanQuBiaoService;
        private readonly It_ZhanDuiBiaoService t_ZhanDuiBiaoService;
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;

        public QuanXianController(It_QuanXianBiaoService quanXianBiaoService, It_QuanXianPeiZhiBiaoService quanXianPeiZhiBiaoService, It_RenYuanQuanXianService renYuanQuanXianService, It_BuMenBiaoService buMenBiaoService, It_GangWeiBiaoService gangWeiBiaoService, It_ZhanQuBiaoService zhanQuBiaoService, It_ZhanDuiBiaoService zhanDuiBiaoService, It_RenYuanBiaoService renYuanBiaoService, IConfiguration configuration)
        {
            t_QuanXianBiaoService = quanXianBiaoService;
            t_QuanXianPeiZhiBiaoService = quanXianPeiZhiBiaoService;
            t_RenYuanQuanXianService = renYuanQuanXianService;
            t_BuMenBiaoService = buMenBiaoService;
            t_GangWeiBiaoService = gangWeiBiaoService;
            t_ZhanQuBiaoService = zhanQuBiaoService;
            t_ZhanDuiBiaoService = zhanDuiBiaoService;
            t_RenYuanBiaoService = renYuanBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

       
        // GET: /<controller>/
        public IActionResult QuanXianGuanLi()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult QuanXianList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult QuanXianPeiZhi()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        #region 权限表


        public string QuanXianFenYe()
        {
            t_QuanXianBiao t_QuanXianBiao = new t_QuanXianBiao();
            string shuju = t_QuanXianBiaoService.YiFenPeiFenYe(t_QuanXianBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
            if (t_QuanXianBiaoService.listshuju().ToList().Count != 0)
            {
                return shuju;
            }
            else
            {
                string jsonString = "{\"draw\":1,\"total\":0,\"total\":0,";
                jsonString += "\"" + "data" + "\":" + "[]" + ",";
                jsonString = jsonString.TrimEnd(',');
                return jsonString + "}";
                //return "[]";
            }
        }
        public string Add()
        {
            string QuanXianMingCheng = Request.Form["QuanXianMingCheng"].ToString();
            t_QuanXianBiao t_QuanXianBiao = new t_QuanXianBiao();
            t_QuanXianBiao.QuanXianMingCheng = QuanXianMingCheng;
            t_QuanXianBiao.BeiZhu1 = "启用";
            t_QuanXianBiaoService.Add(t_QuanXianBiao);
            return "";
        }
        public string ZhuangTaiXG()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);

            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var QuanXian = _context.t_QuanXianBiao.FirstOrDefault(x => x.Id == Id);
                if (QuanXian.BeiZhu1 == "启用")
                {
                    QuanXian.BeiZhu1 = "停用";
                }
                else if (QuanXian.BeiZhu1 == "停用")
                {
                    QuanXian.BeiZhu1 = "启用";
                }
                panduan = _context.SaveChanges() > 0;
            }
            return "";
        }
        public string HuiBang()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            t_QuanXianBiao t_QuanXianBiao = t_QuanXianBiaoService.GetById(Id);
            return t_QuanXianBiao.QuanXianMingCheng;
        }
        public string XiuGai()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string QuanXianMingCheng = Request.Form["QuanXianMingCheng"].ToString();
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var QuanXian = _context.t_QuanXianBiao.FirstOrDefault(x => x.Id == Id);
                QuanXian.QuanXianMingCheng = QuanXianMingCheng;
                panduan = _context.SaveChanges() > 0;
            }
            return "";
        }
        #endregion
        #region 权限配置表
        public string QuanXianPeiZhiFenYe()
        {
            t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = new t_QuanXianPeiZhiBiao();
            string shuju = t_QuanXianPeiZhiBiaoService.YiFenPeiFenYe(t_QuanXianPeiZhiBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
            if (t_QuanXianPeiZhiBiaoService.listshuju().ToList().Count != 0)
            {
                return shuju;
            }
            else
            {
                string jsonString = "{\"draw\":1,\"total\":0,\"total\":0,";
                jsonString += "\"" + "data" + "\":" + "[]" + ",";
                jsonString = jsonString.TrimEnd(',');
                return jsonString + "}";
                //return "[]";
            }
        }
        public string QuanXianPeiZhiAdd()
        {
            string QuanXianPeiZhiMingCheng = Request.Form["QuanXianPeiZhiMingCheng"].ToString();
            t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = new t_QuanXianPeiZhiBiao();
            t_QuanXianPeiZhiBiao.PeiZhiMingCheng = QuanXianPeiZhiMingCheng;
            t_QuanXianPeiZhiBiao.BeiZhu1 = "启用";
            t_QuanXianPeiZhiBiaoService.Add(t_QuanXianPeiZhiBiao);
            return "";
        }
        public string PeiZhiZhuangTaiXG()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var QuanXianPeiZhiBiao = _context.t_QuanXianPeiZhiBiao.FirstOrDefault(x => x.Id == Id);
                if (QuanXianPeiZhiBiao.BeiZhu1 == "启用")
                {
                    QuanXianPeiZhiBiao.BeiZhu1 = "停用";
                }
                else if (QuanXianPeiZhiBiao.BeiZhu1 == "停用")
                {
                    QuanXianPeiZhiBiao.BeiZhu1 = "启用";
                }
                panduan = _context.SaveChanges() > 0;
            }
            return "";
        }
        public string PeiZhiMingChengHuiBang()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = t_QuanXianPeiZhiBiaoService.GetById(Id);
            return t_QuanXianPeiZhiBiao.PeiZhiMingCheng;
        }
        public string PeiZhiMingChengXiuGai()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string QuanXianPeiZhiMingCheng = Request.Form["QuanXianPeiZhiMingCheng"].ToString();


            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var QuanXianPeiZhiBiao = _context.t_QuanXianPeiZhiBiao.FirstOrDefault(x => x.Id == Id);
                QuanXianPeiZhiBiao.PeiZhiMingCheng = QuanXianPeiZhiMingCheng;
                panduan = _context.SaveChanges() > 0;
            }

            return "";
        }
        public string HuiBangDuoXuan()
        {
            int pandaun = Convert.ToInt32(Request.Form["panduan"]);
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string html = "";
            if (pandaun==1)
            {
                List<t_QuanXianBiao> t_QuanXians = t_QuanXianBiaoService.GetList1();
                t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = t_QuanXianPeiZhiBiaoService.GetById(Id);
                if (t_QuanXianPeiZhiBiao.QuanXianId == null || t_QuanXianPeiZhiBiao.QuanXianId == "")
                {
                    for (int i = 0; i < t_QuanXians.Count; i++)
                    {
                        html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                    }
                }
                else
                {
                    string[] shu = t_QuanXianPeiZhiBiao.QuanXianId.Split(",");
                    for (int i = 0; i < t_QuanXians.Count; i++)
                    {
                        bool panduan = true;
                        for (int j = 0; j < shu.Length; j++)
                        {
                            if (shu[j] == t_QuanXians[i].Id.ToString())
                            {
                                panduan = false;
                                break;
                            }
                        }
                        if (panduan)
                        {
                            html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                        }
                        else
                        {
                            html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' checked/><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                        }
                    }
                }
            }
            else if(pandaun==2)
            {
                List<t_QuanXianBiao> t_QuanXians = t_QuanXianBiaoService.GetList1();
                t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = t_QuanXianPeiZhiBiaoService.GetById(Id);
                if (t_QuanXianPeiZhiBiao.QuanXianId == null || t_QuanXianPeiZhiBiao.QuanXianId == "")
                {
                    for (int i = 0; i < t_QuanXians.Count; i++)
                    {
                        html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll1' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                    }
                }
                else
                {
                    string[] shu = t_QuanXianPeiZhiBiao.QuanXianId.Split(",");
                    for (int i = 0; i < t_QuanXians.Count; i++)
                    {
                        bool panduan = true;
                        for (int j = 0; j < shu.Length; j++)
                        {
                            if (shu[j] == t_QuanXians[i].Id.ToString())
                            {
                                panduan = false;
                                break;
                            }
                        }
                        if (panduan)
                        {
                            html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll1' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                        }
                        else
                        {
                            html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll1' value='" + t_QuanXians[i].Id + "' checked/><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                        }
                    }
                }
            }
            return html;
        }
        public string PeiZhiQuanXian()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string QuanXianId = Request.Form["QuanXianId"].ToString();
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var QuanXianPeiZhiBiao = _context.t_QuanXianPeiZhiBiao.FirstOrDefault(x => x.Id == Id);
                QuanXianPeiZhiBiao.QuanXianId = QuanXianId;
                panduan = _context.SaveChanges() > 0;
            }

            return "";
        }
        public string pinjie()
        {
            string shuju = "";
            int Id = Convert.ToInt32(Request.Form["Id"]);
            t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = t_QuanXianPeiZhiBiaoService.GetById(Id);
            int panduan = 10;
            if (t_QuanXianPeiZhiBiao.QuanXianId != null || t_QuanXianPeiZhiBiao.QuanXianId != "")
            {
                string[] shu = t_QuanXianPeiZhiBiao.QuanXianId.Split(",");
                for (int i = 0; i < shu.Length; i++)
                {
                    t_QuanXianBiao t_QuanXianBiao = t_QuanXianBiaoService.GetById(Convert.ToInt32(shu[i]));
                    if (i == 0)
                    {
                        shuju += t_QuanXianBiao.QuanXianMingCheng;
                    }
                    else
                    {
                        if (i >= panduan)
                        {
                            panduan += 10;
                            shuju += "," + t_QuanXianBiao.QuanXianMingCheng + "</br>";
                        }
                        else
                        {
                            shuju += "," + t_QuanXianBiao.QuanXianMingCheng;
                        }
                    }
                }
            }
            return shuju;
        }
        #endregion
        #region 配置人员权限
        public string BangDingMouBan()
        {
            return JsonConvert.SerializeObject(t_QuanXianPeiZhiBiaoService.GetList1(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        public string BangDingBuMen()
        {
            return JsonConvert.SerializeObject(t_BuMenBiaoService.GetList().Where(x=>x.Id!=1).ToList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        public string BangDingGangWei()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            return JsonConvert.SerializeObject(t_RenYuanQuanXianService.BuMenLianDong(Id), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        public string BangDingZhanQu()
        {
            return JsonConvert.SerializeObject(t_ZhanQuBiaoService.GetList().Where(x => x.Id != 1).ToList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        public string BangDingZhanDui()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            return JsonConvert.SerializeObject(t_RenYuanQuanXianService.ZhanDuiLianDong(Id), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        public string HuiBangQuanXian()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string html = "";
            List<t_QuanXianBiao> t_QuanXians = t_QuanXianBiaoService.GetList1();
            List<t_RenYuanQuanXian> t_RenYuanQuans = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Id).ToList();
            if (t_RenYuanQuans.Count == 0)
            {
                t_RenYuanQuanXian chushi = new t_RenYuanQuanXian();
                chushi.RenYuanId = Id;
                t_RenYuanQuanXianService.Add(chushi);
            }
            List<t_RenYuanQuanXian> t_RenYuanQuan = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Id).ToList();
            if (t_RenYuanQuan[0].QuanXianId == null || t_RenYuanQuan[0].QuanXianId == "")
            {
                for (int i = 0; i < t_QuanXians.Count; i++)
                {
                    html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                }
            }
            else
            {
                string[] shu = t_RenYuanQuan[0].QuanXianId.Split(",");
                for (int i = 0; i < t_QuanXians.Count; i++)
                {
                    bool panduan = true;
                    for (int j = 0; j < shu.Length; j++)
                    {
                        if (shu[j] == t_QuanXians[i].Id.ToString())
                        {
                            panduan = false;
                            break;
                        }
                    }
                    if (panduan)
                    {
                        html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                    }
                    else
                    {
                        html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' checked/><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                    }
                }
            }
            return html;
        }
        public string RenYuanPeiZhiQuanXian()
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string QuanXianId = Request.Form["QuanXianId"].ToString();
            List<t_RenYuanQuanXian> t_RenYuanQuan = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Id).ToList();
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var RenYuanQuanXian = _context.t_RenYuanQuanXian.FirstOrDefault(x => x.Id == t_RenYuanQuan[0].Id);
                RenYuanQuanXian.QuanXianId = QuanXianId;
                panduan = _context.SaveChanges() > 0;
            }
            return "";
        }
        public string InitializationDataTable()
        {
            string BuMen = Request.Form["BuMen"];
            string GangWei = Request.Form["GangWei"];
            string ZhanQu = Request.Form["ZhanQu"];
            string XingMing = Request.Form["XingMing"];
            string ZhanDui = Request.Form["ZhanDui"];
            string ShouJiHao = Request.Form["ShouJiHao"];
            //t_RenYuanBiao t_renYuanBiao = new t_RenYuanBiao();
            //if (BuMen != "")
            //{
            //    if (BuMen != "0")
            //    {
            //        string bumentiaojian = "(";
            //        List<t_GangWeiBiao> t_GangWeiBiaos = t_GangWeiBiaoService.GetList().Where(x => x.BuMenId == Convert.ToInt32(BuMen)).ToList();
            //        for (int i = 0; i < t_GangWeiBiaos.Count; i++)
            //        {
            //            if (i == 0)
            //            {
            //                bumentiaojian += "GangWeiId=" + t_GangWeiBiaos[i].Id + "";

            //            }
            //            else
            //            {
            //                bumentiaojian += " or GangWeiId=" + t_GangWeiBiaos[i].Id + "";
            //            }
            //        }
            //        bumentiaojian += ")";
            //        t_renYuanBiao.BeiZhu10 = bumentiaojian;
            //    }
            //}
            //if (GangWei != "")
            //{
            //    if (GangWei != "0")
            //    {
            //        t_renYuanBiao.GangWeiId = Convert.ToInt32(GangWei);
            //    }
            //}
            //if (ZhanQu != "")
            //{
            //    if (ZhanQu != "0")
            //    {
            //        string zhanqutiaojian = "(";
            //        List<t_ZhanDuiBiao> t_ZhanDuiBiao = t_ZhanDuiBiaoService.GetList().Where(x => x.ZhanQuId == Convert.ToInt32(ZhanQu)).ToList();
            //        for (int i = 0; i < t_ZhanDuiBiao.Count; i++)
            //        {
            //            if (i == 0)
            //            {
            //                zhanqutiaojian += "ZhanDuiId=" + t_ZhanDuiBiao[i].id + "";

            //            }
            //            else
            //            {
            //                zhanqutiaojian += " or ZhanDuiId=" + t_ZhanDuiBiao[i].id + "";
            //            }
            //        }
            //        zhanqutiaojian += ")";
            //        t_renYuanBiao.BeiZhu9 = zhanqutiaojian;
            //    }
            //}
            //if (XingMing != "")
            //{
            //    t_renYuanBiao.XingMing = XingMing;
            //}
            //if (ZhanDui != "")
            //{
            //    if (ZhanDui != "0")
            //    {
            //        t_renYuanBiao.ZhanDuiId = Convert.ToInt32(ZhanDui);
            //    }
            //}
            //if (ShouJiHao != "")
            //{
            //    t_renYuanBiao.LianXiFangShi = ShouJiHao;
            //}
            t_RenYuanBiao t_renYuanBiao = new t_RenYuanBiao();
            //t_renYuanBiao.BeiZhu9 = Convert.ToInt32(Request.Form["selGongSi"]).ToString();
            if (BuMen!="" && BuMen!="0")
            {
                t_renYuanBiao.BeiZhu10 = BuMen;
            }
            if (GangWei != "" && GangWei != "0")
            {
                t_renYuanBiao.GangWeiId = Convert.ToInt32(GangWei);
            }
            if (ZhanQu != "" && ZhanQu != "0")
            {
                t_renYuanBiao.ZhanQuId = Convert.ToInt32(ZhanQu);
            }
            if (ZhanDui != "" && ZhanDui != "0")
            {
                t_renYuanBiao.ZhanDuiId = Convert.ToInt32(ZhanDui);
            }
            if (XingMing != "")
            {
                t_renYuanBiao.XingMing = XingMing;
            }
            
            return t_RenYuanBiaoService.GetListWithFenYe1(t_renYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            //t_renYuanBiao.BeiZhu1 = "";
            //string a = t_RenYuanQuanXianService.GetListWithFenYe(t_renYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            //return t_RenYuanQuanXianService.GetListWithFenYe(t_renYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));   
        }
        public string PinJieRenYuan()
        {
            string RenYuanId = Request.Form["RenYuanId"];
            string[] shu = RenYuanId.Split(",");
            string pinjie = "";
            for (int i = 0; i < shu.Length; i++)
            {
                if (shu[i]!="")
                {
                    t_RenYuanBiao t_RenYuanBiao = t_RenYuanBiaoService.GetById(Convert.ToInt32(shu[i]));
                    pinjie += "<button id='shan' type='button' style='' class='btn btn-outline btn-default' onclick='ShanChu("+ t_RenYuanBiao.Id+ ")'>"+t_RenYuanBiao.XingMing+"×</button>";
                }
            }
            return pinjie;
        }
        public string RenYuanQuanXianPeiZhi() 
        {
            string html = "";
            List<t_QuanXianBiao> t_QuanXians = t_QuanXianBiaoService.GetList1();
            for (int i = 0; i < t_QuanXians.Count; i++)
            {
                html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll1' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
            }
            return html;
        }
        public string RenYuanPiLiangPeiZhiQuanXian()
        {
            string RenYuanId = Request.Form["RenYuanId"].ToString();
            string QuanXianId = Request.Form["QuanXianId"].ToString();
            string[] shu = RenYuanId.Split(",");
            for (int i = 0; i < shu.Length; i++)
            {
                List<t_RenYuanQuanXian> t_RenYuanQuans = t_RenYuanQuanXianService.GetList().Where(x => x.RenYuanId == Convert.ToInt32(shu[i])).ToList();
                if (t_RenYuanQuans.Count == 0)
                {
                    t_RenYuanQuanXian chushi = new t_RenYuanQuanXian();
                    chushi.RenYuanId = Convert.ToInt32(shu[i]);
                    chushi.QuanXianId = QuanXianId;
                    t_RenYuanQuanXianService.Add(chushi);
                }
                else
                {
                    bool panduan = false;
                    var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                    optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var RenYuanQuanXian = _context.t_RenYuanQuanXian.FirstOrDefault(x => x.Id == t_RenYuanQuans[0].Id);
                        RenYuanQuanXian.QuanXianId = QuanXianId;
                        panduan = _context.SaveChanges() > 0;
                    }
                }
            }
            return "";
        }
        public string HuiBangDuoXuan1() 
        {
            int Id = Convert.ToInt32(Request.Form["Id"]);
            string html = "";
            List<t_QuanXianBiao> t_QuanXians = t_QuanXianBiaoService.GetList1();
            t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = t_QuanXianPeiZhiBiaoService.GetById(Id);
            if (t_QuanXianPeiZhiBiao.QuanXianId == null || t_QuanXianPeiZhiBiao.QuanXianId == "")
            {
                for (int i = 0; i < t_QuanXians.Count; i++)
                {
                    html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                }
            }
            else
            {
                string[] shu = t_QuanXianPeiZhiBiao.QuanXianId.Split(",");
                for (int i = 0; i < t_QuanXians.Count; i++)
                {
                    bool panduan = true;
                    for (int j = 0; j < shu.Length; j++)
                    {
                        if (shu[j] == t_QuanXians[i].Id.ToString())
                        {
                            panduan = false;
                            break;
                        }
                    }
                    if (panduan)
                    {
                        html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' /><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                    }
                    else
                    {
                        html += "<div class='form-group col-sm-3'><div class='checkbox-custom checkbox-inline checkbox-primary float-left'><input type='checkbox' id='checkAll" + i + "' name='checkAll' value='" + t_QuanXians[i].Id + "' checked/><label for='checkAll" + i + "'>" + t_QuanXians[i].QuanXianMingCheng + "</label></div></div>";
                    }
                }
            }
            return html;
        }
        #endregion

        public string QuanXianGuanLiPanDuan() 
        {
            string shuju = HttpContext.Session.GetString("loginid");
            if (shuju != "" && shuju != null)
            {
                int id = Convert.ToInt32(shuju);
                t_RenYuanQuanXian qx = t_RenYuanQuanXianService.Quanxiid(id);
                StringBuilder Json = new StringBuilder();
                Json.Append("[");
                string quanxian = qx.QuanXianId;
                string[] quanxianlist = qx.QuanXianId.ToString().Split(",");
                for (int i = 0; i < quanxianlist.Length; i++)
                {
                    Json.Append("{\"id\":\"" + quanxianlist[i] + "\"}");
                    if ((i + 1) == quanxianlist.Length)
                    {

                    }
                    else
                    {
                        Json.Append(",");
                    }
                }
                Json.Append("]");
                return Json.ToString();
            }
            else
            {
                return "[]";
            }
        }
    }
}
